package cn.elwy.rcp.ui.table.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;

import cn.elwy.rcp.ui.table.TablePane;
import cn.elwy.rcp.ui.table.model.ColumnInfo;
import cn.elwy.rcp.ui.table.model.TableInfo;

public class TableEditorAdapter {

  protected TablePane tablePane;
  protected TableInfo tableInfo;
  private String[] columnNames;
  private Map<String, ColumnInfo> columnInfoMap;

  public final void init() {
    ColumnViewer viewer = tablePane.getTableViewer();
    DoubleClickEditorActivationStrategy doubleClickStrategy = new DoubleClickEditorActivationStrategy(viewer);
    if (viewer instanceof TableViewer) {
      TableViewerEditor.create((TableViewer) viewer, doubleClickStrategy, ColumnViewerEditor.DEFAULT);
    } else {
      TreeViewerEditor.create((TreeViewer) viewer, doubleClickStrategy, ColumnViewerEditor.DEFAULT);
    }
    viewer.setColumnProperties(getColumnProperties());
    createColumnEditor();
  }

  public void resetEditorTable() {
  }

  protected void createColumnEditor() {
    ColumnViewer viewer = tablePane.getTableViewer();

    ICellModifier cellModifier = getCellModifier();
    if (cellModifier != null) {
      viewer.setCellModifier(cellModifier);
    }
    CellEditor[] cellEditors = getCellEditors();
    if (cellEditors != null) {
      viewer.setCellEditors(cellEditors);
    }
  }

  protected CellEditor[] getCellEditors() {
    List<ColumnInfo> columnInfoList = tableInfo.getColumnInfoList();
    CellEditor[] CellEditor = new CellEditor[columnInfoList.size()];
    for (int i = 0; i < columnInfoList.size(); i++) {
      ColumnInfo columnInfo = columnInfoList.get(i);
      if (columnInfo.isCanEditor()) {
        CellEditor[i] = new TextCellEditor((Composite) tablePane.getTable(), SWT.SINGLE);
      }
    }
    return CellEditor;
  }

  protected ICellModifier getCellModifier() {
    return new CellModifierAdapter(this);
  }

  private final String[] getColumnProperties() {
    List<ColumnInfo> columnInfoList = tableInfo.getColumnInfoList();
    columnNames = new String[columnInfoList.size()];
    columnInfoMap = new HashMap<String, ColumnInfo>();
    for (int i = 0; i < columnInfoList.size(); i++) {
      ColumnInfo columnInfo = columnInfoList.get(i);
      columnNames[i] = columnInfo.getId();
      columnInfoMap.put(columnInfo.getId(), columnInfo);
    }
    return columnNames;
  }

  public TablePane getTablePane() {
    return tablePane;
  }

  public void setTablePane(TablePane tablePane) {
    this.tablePane = tablePane;
    this.tableInfo = tablePane.getTableInfo();
  }

  public Map<String, ColumnInfo> getColumnInfoMap() {
    return columnInfoMap;
  }

}
